/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import POJO.TheLoaiPhimPOJO;
import com.mysql.jdbc.CallableStatement;
import com.mysql.jdbc.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author kimloan
 */
public class TheLoaiPhimDAO {
 
    public boolean themTheLoaiPhim(TheLoaiPhimPOJO tlp) {
        boolean kq = false;
        String sql = String.format("INSERT INTO theloaiphim(TenTheLoaiPhim)"
                + " VALUES('%s')", tlp.getTenTheLoaiPhim());
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        int n = helper.executeUpdate(sql);
        if (n == 1) {
            kq = true;//Them TheLoaiPhim thanh cong
        }

        helper.close();
        return kq;
    }

    /**
     * Phuong Thuc Xoa TheLoaiPhim
     * @return
     */
    public boolean xoaTheLoaiPhim(String maTheLoaiPhim) {

        boolean kq = false;
        try {
            String sql = "delete from theloaiphim where idTheLoaiPhim=" + maTheLoaiPhim;
            // String sql = "DELETE FROM TheLoaiPhim WHERE maTheLoaiPhim ='"+  maTheLoaiPhim+"'";
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            int n = helper.executeUpdate(sql);
            if (n >= 1) {
                kq = true;//xoa thanh cong
            }
            helper.close();
            return kq;
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return kq;
        }
    }

    /**
     * Phuong Thuc Cap Nhat TheLoaiPhim
     */
    public boolean capNhatTheLoaiPhim(TheLoaiPhimPOJO tlp) {
        boolean kq = false;

        String sql = String.format(
                "UPDATE theloaiphim SET TenTheLoaiPhim='%s'"
                + " WHERE idTheLoaiPhim=%d ", tlp.getTenTheLoaiPhim(),tlp.getMaTheLoaiPhim());
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        int n = helper.executeUpdate(sql);
        if (n == 1) {
            kq = true;//Cap Nhat TheLoaiPhim thanh cong
        }
        helper.close();
        return kq;
    }

    /**
     * Phuong Thuc Xem Danh TheLoaiPhim
     * @return
     */
    public ArrayList<TheLoaiPhimPOJO> layDanhTheLoaiPhimCacMaTheLoaiPhim() {
        ArrayList<TheLoaiPhimPOJO> ds = new ArrayList<TheLoaiPhimPOJO>();
        try {
            String sql = "SELECT * FROM theloaiphim";
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()) {
                TheLoaiPhimPOJO tlp = new TheLoaiPhimPOJO();
                tlp.setMaTheLoaiPhim(rs.getInt("idTheLoaiPhim"));
                tlp.setTenTheLoaiPhim(rs.getString("TenTheLoaiPhim"));
                ds.add(tlp);
            }
            helper.close();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return ds;
    }

    /**
     * Phuong Thuc Lay Thong Tin TheLoaiPhim
     * @param maTheLoaiPhim
     * @return
     */
    public TheLoaiPhimPOJO layThongTinTheLoaiPhim(String maTheLoaiPhim) {
        TheLoaiPhimPOJO tlp = null;
        String sql = String.format("SELECT * FROM theloaiphim WHERE idTheLoaiPhim=%d", maTheLoaiPhim);
        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            if (rs.next()) {
                tlp = new TheLoaiPhimPOJO();
                tlp.setMaTheLoaiPhim(rs.getInt("idTheLoaiPhim"));
                tlp.setTenTheLoaiPhim(rs.getString("TenTheLoaiPhim"));
            }
            helper.close();
        } catch (SQLException ex) {

            System.out.println(ex.getMessage());
        }
        return tlp;
    }
    //Ham Lay danh TheLoaiPhim theo the loai

    public ArrayList<TheLoaiPhimPOJO> layDSTheLoaiPhim_TheoTheLoai(int maTheLoai) {

        ArrayList<TheLoaiPhimPOJO> dsTheLoaiPhim = new ArrayList<TheLoaiPhimPOJO>();
        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            Connection cn = (Connection) helper.getConnection();
            CallableStatement statement = (CallableStatement) cn.prepareCall("{call sp_LayDSTheLoaiPhim_TheoTheLoai(?)}");
            statement.setInt(1, maTheLoai);

            statement.execute();
            ResultSet rs = statement.getResultSet();
            while (rs.next()) {
                TheLoaiPhimPOJO tlp = new TheLoaiPhimPOJO();
                tlp.setMaTheLoaiPhim(rs.getInt("idTheLoaiPhim"));
                tlp.setTenTheLoaiPhim(rs.getString("TenTheLoaiPhim"));
                dsTheLoaiPhim.add(tlp);

            }
            helper.close();
            return dsTheLoaiPhim;
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
            return dsTheLoaiPhim;
        }
    }

    public ArrayList<TheLoaiPhimPOJO> timKiemTheLoaiPhim_TheoTenTheLoaiPhim(String _tenTheLoaiPhim) {

        ArrayList<TheLoaiPhimPOJO> ds = new ArrayList<TheLoaiPhimPOJO>();
        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            Connection cn = (Connection) helper.getConnection();
            CallableStatement statement = (CallableStatement) cn.prepareCall("{call sp_LayTheLoaiPhimTheoTenTheLoaiPhim(?)}");
            statement.setString(1, _tenTheLoaiPhim);
            statement.execute();
            ResultSet rs = statement.getResultSet();
            while (rs.next()) {
                TheLoaiPhimPOJO tlp = new TheLoaiPhimPOJO();
                tlp.setMaTheLoaiPhim(rs.getInt("idTheLoaiPhim"));
                tlp.setTenTheLoaiPhim(rs.getString("TenTheLoaiPhim"));
                ds.add(tlp);
            }
            helper.close();
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
        }
        return ds;
    }
}
